<?php

include_once 'common.php';
include_once 'qywx_robot_notify.php';

$subjects = json_stringify($payload->subjects);
$traineeLicences = json_stringify($payload->traineeLicences);
$subjectsComments = json_stringify($payload->subjectsComments);
$failedSubjects = json_stringify($payload->failedSubjects);
$evaluations = json_stringify($payload->evaluations);
$history = json_stringify($payload->history);
$currentTime = date('Y-m-d H:i:s');
$sql = '';
if ($payload->number) {
  // update
  $sql = "UPDATE `trex_records` SET `title`='{$payload->title}',`year`={$payload->year},`cycle`={$payload->cycle},`type`={$payload->type},`office`='{$payload->office}',`subjects`='{$subjects}',`hours`={$payload->hours},`pass`='{$payload->pass}',`traineeNumber`={$payload->traineeNumber},`traineeName`='{$payload->traineeName}',`traineeDepartment`='{$payload->traineeDepartment}',`traineeOffice`='{$payload->traineeOffice}',`traineeGroup`='{$payload->traineeGroup}',`traineeGrade`='{$payload->traineeGrade}',`traineePost`='{$payload->traineePost}',`traineeLicences`='{$traineeLicences}',`traineeComments`='{$payload->traineeComments}',`coachNumber`={$payload->coachNumber},`coachName`='{$payload->coachName}',`subjectsComments`='{$subjectsComments}',`coachComments`='{$payload->coachComments}',`failedSubjects`='{$failedSubjects}',`failedSubjectsCount`={$payload->failedSubjectsCount},`evaluations`='{$evaluations}',`evaluationMin`={$payload->evaluationMin},`evaluationScore`={$payload->evaluationScore},`trainingDate`='{$payload->trainingDate}',`history`='{$history}',`status`={$payload->status},`commitTime`='{$payload->commitTime}',`updateTime`='{$currentTime}' WHERE `number` = {$payload->number}";
  syncSecurityData($payload);
} else {
  // insert
  $sql = "INSERT INTO `trex_records`(`exerciseID`, `title`, `year`, `cycle`, `type`, `office`, `subjects`, `hours`, `pass`, `traineeNumber`, `traineeName`, `traineeDepartment`, `traineeOffice`, `traineeGroup`, `traineeGrade`, `traineePost`, `traineeLicences`, `traineeComments`, `coachNumber`, `coachName`, `subjectsComments`, `coachComments`, `failedSubjects`, `failedSubjectsCount`, `evaluations`, `evaluationMin`, `evaluationScore`, `trainingDate`, `history`, `status`, `commitTime`, `updateTime`) VALUES ({$payload->exerciseID},'{$payload->title}',{$payload->year},{$payload->cycle},{$payload->type},'{$payload->office}','{$subjects}',{$payload->hours},'{$payload->pass}',{$payload->traineeNumber},'{$payload->traineeName}','{$payload->traineeDepartment}','{$payload->traineeOffice}','{$payload->traineeGroup}','','','[]','',{$payload->coachNumber},'{$payload->coachName}','{$subjectsComments}','{$payload->coachComments}','{$failedSubjects}',{$payload->failedSubjectsCount},'{$evaluations}',{$payload->evaluationMin},{$payload->evaluationScore},'{$payload->trainingDate}','{$history}',{$payload->status},'{$currentTime}','{$currentTime}')";
}
$result = exec_sql($sql);
send_msg(general_message($user, $payload));
output(['sql' => $sql, 'result' => $result, 'payload' => $payload]);

function syncSecurityData($payload) {
  if (strlen($payload->traineeOffice) > 0 && strlen($payload->traineeGroup) > 0) {
    $sql = "UPDATE `oprec_security_data` SET `clientOffice`='{$payload->traineeOffice}',`clientGroup`='{$payload->traineeGroup}' WHERE `number` = {$payload->traineeNumber}";
    return exec_sql($sql);
  }
}

function general_message($user, $payload) {
  $msg = "";
  $payload->number ? $msg .= "{$user->name}更新了培训记录No.{$payload->number}：" : $msg .= "{$user->name}创建了培训记录";
  $msg .= "\n练习：{$payload->title}";
  $msg .= "\n科室：{$payload->office}";
  $msg .= "\n年度：{$payload->year}年";
  $payload->cycle == -1 ? $msg .= "\n周期：不计入周期" : $msg .= "\n周期：第{$payload->cycle}期";
  $payload->type == 1 ? $msg .= "\n类型：年度复训" : $msg .= "\n类型：专项培训";
  $msg .= "\n日期：{$payload->trainingDate}";
  $msg .= "\n学员：{$payload->traineeName}";
  $msg .= "\n教员：{$payload->coachName}";
  $msg .= "\n成绩：{$payload->evaluationScore}分";
  $msg .= "\n通过：{$payload->pass}";
  strlen($payload->traineeComments) > 0 ? $msg .= "\n自评：已完成" : $msg .= "\n自评：未完成";
  return $msg;
}

function send_msg($msg) {
  $res = robot_notify($msg);
  if (IS_DEBUG) {
    log_text(var_export($res, true));
  }
  return $res;
}